﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.HtmlControls;

using UncleBobWebService.Models;
using UncleBobWebService.ServiceRequester;

namespace UncleBobApplicantWebService.Controllers
{
    public class EditQuestionController : Controller
    {
        private UncleBobApplicantWebService.ApplicantWebService.WebServiceClient mApplicantClient = new UncleBobApplicantWebService.ApplicantWebService.WebServiceClient();        
        //
        // GET: /EditQuestion/

        public ActionResult EditQuestion()
        {                       
            return View(Session[SessionKeys.QUESTION]);
        }
        
        public ActionResult FormAction(FormCollection collection)
        {            
            if (collection["questionSubmit"] != null)
            {
                string question = collection.Get("QuestionText");

                if (ValueNotNull(collection, "answer1") | Session[SessionKeys.QUESTION].GetType() == typeof(MultipleChoiceQuestion) )
                {
                    MultipleChoiceQuestion mQ = new MultipleChoiceQuestion();                    
                    mQ.Question = question;
                    mQ.QuestionID = (Session[SessionKeys.QUESTION] as MultipleChoiceQuestion).QuestionID;

                    if (ValueNotNull(collection, "Answer1"))
                    {
                        Answer a = new Answer(collection.Get("Answer1"), IsChecked(collection, "AnswerCheckBox1"));
                        a.ID = (Session[SessionKeys.QUESTION] as MultipleChoiceQuestion).PossibleAnswers[0].ID;
                        mQ.AddPossibleAnswer(a);
                    }

                    if (ValueNotNull(collection, "Answer2"))
                    {
                        Answer a = new Answer(collection.Get("Answer2"), IsChecked(collection, "AnswerCheckBox2"));
                        a.ID = (Session[SessionKeys.QUESTION] as MultipleChoiceQuestion).PossibleAnswers[1].ID;
                        mQ.AddPossibleAnswer(a);
                    }

                    if (ValueNotNull(collection, "Answer3"))
                    {
                        Answer a = new Answer(collection.Get("Answer3"), IsChecked(collection, "AnswerCheckBox3"));
                        a.ID = (Session[SessionKeys.QUESTION] as MultipleChoiceQuestion).PossibleAnswers[2].ID;
                        mQ.AddPossibleAnswer(a);
                    }

                    if (ValueNotNull(collection, "Answer4"))
                    {
                        Answer a = new Answer(collection.Get("Answer4"), IsChecked(collection, "AnswerCheckBox4"));
                        a.ID = (Session[SessionKeys.QUESTION] as MultipleChoiceQuestion).PossibleAnswers[3].ID;
                        mQ.AddPossibleAnswer(a);
                    }

                    if (ValueNotNull(collection, "Answer5"))
                    {
                        Answer a = new Answer(collection.Get("Answer5"), IsChecked(collection, "AnswerCheckBox5"));
                        a.ID = (Session[SessionKeys.QUESTION] as MultipleChoiceQuestion).PossibleAnswers[4].ID;
                        mQ.AddPossibleAnswer(a);
                    }

                    if (mQ.PossibleAnswers.Count >= 1)
                    {
                        try
                        {

                            Message msg = new Message(MessageTypes.SERVICE_EDIT_JOB_QUESTION);
                            msg.Data = mQ;
                            mApplicantClient.RequestService(msg);

                            if (!msg.Result)
                            {
                                //TempData(SessionKeys.ERROR) = "Unable to save questions to the database.";

                            }
                        }
                        catch (Exception ex)
                        {
                            Session.Add(SessionKeys.ERROR, "Unable to save the edit to the question. " + ex.Message);
                            return RedirectToAction("Error", "Error");
                        }
                    }
                    else
                    {
                        // This handles when a user changes the question type.
                        ExtendedQuestion eX = new ExtendedQuestion(question);
                        eX.QuestionID = mQ.QuestionID;
                        eX.Question = mQ.Question;

                        Message msg = new Message(MessageTypes.SERVICE_EDIT_JOB_QUESTION);
                        msg.Data = eX;
                        mApplicantClient.RequestService(msg);
                    }
                }
                else
                {
                    ExtendedQuestion eX = new ExtendedQuestion(question);
                    eX.QuestionID = (Session[SessionKeys.QUESTION] as ExtendedQuestion).QuestionID;

                    Message msg = new Message(MessageTypes.SERVICE_EDIT_JOB_QUESTION);
                    msg.Data = eX;
                    mApplicantClient.RequestService(msg);

                    if (!msg.Result)
                    {
                        //TempData(SessionKeys.ERROR) = "Unable to save questions to the database.";

                    }
                }                               
            }

            return RedirectToAction("QuestionsMenu", "QuestionsMenu");  
        }

        private bool IsChecked(FormCollection collection, string key)
        {
            if (collection.Get(key) != null)
                return true;
            return false;
        }

        private bool IsNotNull(FormCollection collection, string key)
        {
            if (collection.Get(key) != null)
                return true;
            return false;
        }

        private bool ValueNotNull(FormCollection collection, string key)
        {
            bool result = false;
            if (collection.Get(key) != null)
            {
                if (collection[key] != string.Empty)
                    result = true;
            }

            return result;
        }

    }
}
